from PySide6.QtCore import Qt, QTime, Slot
from PySide6.QtWidgets import QDialog, QMessageBox

from Devices.Chiot.BsDevs.ComDev.ComDev import ComDev
from Components.MessageBoxFunc import MessageBoxFunc

# class TimerSetWindow(QDialog):
#     def __init__(self, parent = None):
#         super().__init__(parent)
        # self._ui = Ui_FormTimerSet()
        # self._ui.setupUi(self)

        # flags = self.windowFlags()
        # self.setWindowFlags(flags & ~Qt.WindowContextHelpButtonHint)
class ComTimerSetWindow():
    def __init__(self):
        self._ui = None

    def GetTimerServices(self, timer):
        """
        # TODO
        必须实现
        """
        # services = []
        # services.append(ComDev.ServiceParams("switch", "on", self._ui.comboBox_timerSwitchCtrl.currentIndex()))
        # timer.services = services
        pass

    def GetTimer(self, timer):
        #定时器ID
        timer.id = self._ui.lineEdit_timerId.text().strip()
        #定时器启用
        timer.enable = self._ui.checkBox_timerEnable.isChecked()
        #开始时间
        timer.start = self._ui.timeEdit_timerExecTime.text().strip()
        # #结束时间
        # timer.end = self._ui.timeEdit_TimerStopTime.text().strip()

        # 服务
        self.GetTimerServices(timer)

        #执行周期
        if self._ui.checkBox_NoRepeat.isChecked():
            timer.week = 128
        else:
            if self._ui.checkBox_Mon.isChecked():
                timer.week |= int(ComDev.DeviceWeak.WEAK_BIT_MON)
            if self._ui.checkBox_Tue.isChecked():
                timer.week |= int(ComDev.DeviceWeak.WEAK_BIT_TUE)
            if self._ui.checkBox_Wed.isChecked():
                timer.week |= int(ComDev.DeviceWeak.WEAK_BIT_WED)
            if self._ui.checkBox_Thu.isChecked():
                timer.week |= int(ComDev.DeviceWeak.WEAK_BIT_THU)
            if self._ui.checkBox_Fri.isChecked():
                timer.week |= int(ComDev.DeviceWeak.WEAK_BIT_FRI)
            if self._ui.checkBox_Sat.isChecked():
                timer.week |= int(ComDev.DeviceWeak.WEAK_BIT_SAT)
            if self._ui.checkBox_Sun.isChecked():
                timer.week |= int(ComDev.DeviceWeak.WEAK_BIT_SUN)
	
    def SetTimerServices(self, timer):
        """
        # TODO
        必须实现
        """
        # for service in timer.services:
        #     if "switch" == service.serviceId:
        #         self._ui.comboBox_timerSwitchCtrl.setCurrentIndex(service.paraValue)
        #     else:
        #         MessageBoxFunc.ShowMessageBox("编辑定时器","未知的服务ID:%s" % (service.serviceId), self, QMessageBox.Critical)
        pass

    def SetTimer(self, timer):
        #定时器ID
        self._ui.lineEdit_timerId.setText(timer.id)
        #定时器启用
        self._ui.checkBox_timerEnable.setChecked(timer.enable)
        #开始时间
        self._ui.timeEdit_timerExecTime.setTime(QTime.fromString(timer.start))
        #执行控制
        self.SetTimerServices(timer)
        
        #执行周期
        if timer.week > 127:
            self._ui.checkBox_NoRepeat.setChecked(True)
        else:
            self._ui.checkBox_Mon.setChecked(timer.week & int(ComDev.DeviceWeak.WEAK_BIT_MON))
            self._ui.checkBox_Tue.setChecked(timer.week & int(ComDev.DeviceWeak.WEAK_BIT_TUE))
            self._ui.checkBox_Wed.setChecked(timer.week & int(ComDev.DeviceWeak.WEAK_BIT_WED))
            self._ui.checkBox_Thu.setChecked(timer.week & int(ComDev.DeviceWeak.WEAK_BIT_THU))
            self._ui.checkBox_Fri.setChecked(timer.week & int(ComDev.DeviceWeak.WEAK_BIT_FRI))
            self._ui.checkBox_Sat.setChecked(timer.week & int(ComDev.DeviceWeak.WEAK_BIT_SAT))
            self._ui.checkBox_Sun.setChecked(timer.week & int(ComDev.DeviceWeak.WEAK_BIT_SUN))

    @Slot()
    def on_okButton_clicked(self):
        checked = self._ui.checkBox_Mon.isChecked() or \
            self._ui.checkBox_Tue.isChecked() or \
            self._ui.checkBox_Wed.isChecked() or \
            self._ui.checkBox_Thu.isChecked() or \
            self._ui.checkBox_Fri.isChecked() or \
            self._ui.checkBox_Sat.isChecked() or \
            self._ui.checkBox_Sun.isChecked() or \
            self._ui.checkBox_NoRepeat.isChecked()

        if not checked:
            self.SetRepeatStyleSheet("color:red;")
            MessageBoxFunc.ShowMessageBox("定时器设置","请选择一个执行周期!", self, QMessageBox.Warning)
            return
        
        # if None == self._ui.:
        #     self.SetRepeatStyleSheet("color:red;")
        #     MessageBoxFunc.ShowMessageBox("定时器设置","请选择一个执行周期!", self, QMessageBox.Warning)
        #     return
        self.accept()
        
    @Slot()
    def on_cancelButton_clicked(self):
        self.reject()

    @Slot(int, bool)
    def on_buttonGroup_week_idToggled(self, state, checked):
        if checked:
            self._ui.checkBox_NoRepeat.setChecked(False)
            self.SetRepeatStyleSheet("color:black;")

    @Slot(int)
    def on_checkBox_NoRepeat_stateChanged(self, state):
        self._ui.checkBox_Mon.setChecked(not state)
        self._ui.checkBox_Tue.setChecked(not state)
        self._ui.checkBox_Wed.setChecked(not state)
        self._ui.checkBox_Thu.setChecked(not state)
        self._ui.checkBox_Fri.setChecked(not state)
        self._ui.checkBox_Sat.setChecked(not state)
        self._ui.checkBox_Sun.setChecked(not state)
        self.SetRepeatStyleSheet("color:black;")

    def SetRepeatStyleSheet(self, styleSheet):
        self._ui.label_Repeat.setStyleSheet(styleSheet)
        self._ui.checkBox_Mon.setStyleSheet(styleSheet)
        self._ui.checkBox_Tue.setStyleSheet(styleSheet)
        self._ui.checkBox_Wed.setStyleSheet(styleSheet)
        self._ui.checkBox_Thu.setStyleSheet(styleSheet)
        self._ui.checkBox_Fri.setStyleSheet(styleSheet)
        self._ui.checkBox_Sat.setStyleSheet(styleSheet)
        self._ui.checkBox_Sun.setStyleSheet(styleSheet)
        self._ui.checkBox_NoRepeat.setStyleSheet(styleSheet)